package com.blackline.auto.NewDashboard;


import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.*;

import static org.junit.Assert.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.*;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to New Dashboard module.
 */
public class NewDashboard_smoketest {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(NewDashboard_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
	}

	@Test
	public void NewDashboardsmoketest() throws Exception {
		TestData testdata= new TestData("TestData/Match_smoketest.xml");
		int sleepTime = 3000;
		int numberOfWidgets1, numberOfWidgets2;
		
		UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
        
		//REPORTS>Dashboards
		UI.NavigationMenu.REPORTS.click();
		UI.Reports.Dashboards_link.click();
		
		//Check how many widgets are there in the page.
		numberOfWidgets1 = UI.Dashboards.numberOfWidgets(driver);
		
		//Add a new dashboard
		UI.Dashboards.Add_Widget_link.click();
		UI.Dashboards.Add_button.click();
		Thread.sleep(sleepTime);
		//Check how many widgets are there in the page again.
		numberOfWidgets2 = UI.Dashboards.numberOfWidgets(driver);

		if (numberOfWidgets2 == numberOfWidgets1 + 1)
		{
			logger.info("[Pass!] 1 widget is created successfully.");
		}
		else
		{
			logger.error("[Fail!] Failed to create 1 widget.");
			verificationErrors.append("[Fail!] Failed to create 1 widget.");
		}
		
		Thread.sleep(sleepTime);
		UI.Dashboards.Exit_the_dashboard_preview_link.click();
	}
	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.close();
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}